package com.work.keep.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.work.keep.R;
import com.work.keep.adapter.MyListViewAdapter;
import com.work.keep.base.BaseFragment;
import com.work.keep.datas.NewestData;
import com.work.keep.volley.GsonRequest;
import com.work.keep.volley.VolleySingleton;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by dllo on 16/1/16.
 */
public class NewestFragment extends BaseFragment {
    private Context context;
    private ListView listView;
    private MyListViewAdapter myListViewAdapter;
    private VolleySingleton volleySingleton;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = LayoutInflater.from(context).inflate(R.layout.fragment_newest, container, false);
        return v;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        listView = (ListView) view.findViewById(R.id.newestListView);
    }

    @Override
    protected void init() {
        String url = "https://api.gotokeep.com/v1.1/timeline/trending?filter=true";
        myListViewAdapter = new MyListViewAdapter(context);
        listView.setAdapter(myListViewAdapter);
        volleySingleton = VolleySingleton.getInstance();
        GsonRequest<NewestData> newestDataGsonRequest = new GsonRequest<NewestData>(url, NewestData.class, new Response.Listener<NewestData>() {
            @Override
            public void onResponse(NewestData response) {
                myListViewAdapter.addNewestDataLists(response.getData());
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> newestHeader = new HashMap<>();
                newestHeader.put("Authorization", "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJfaWQiOiI1NjY1MDBlMTYzZGIwYzlmNTdiMTQwYWQiLCJ1c2VybmFtZSI6Iuaeq-WQmyIsImF2YXRhciI6Imh0dHA6Ly93eC5xbG9nby5jbi9tbW9wZW4vc2t3MmhSR3lXRWcwUzV0NzhkMlRVUnFhYjBzcWJ4RXB4S1BLUFpEOXpwbTNJbkIySzRjRnhLVEJsemdDb3VIcGlia3RkRUluQ0NFcGpIS2FHZ1lwRkp6SVRIVU5NYTNpYmYvMCIsImlhdCI6MTQ1MjkzMDAxOCwiZXhwIjoxNDU1NTIyMDE4LCJpc3MiOiJodHRwOi8vd3d3LmdvdG9rZWVwLmNvbS8ifQ.gMaASt8gQO3EbffVgZ5M4fAiGfbPu1LK-1OK8osTsTE");
                newestHeader.put("X-X", "486bba2566a02d3ae31ed4445366a873b89cf910");
                newestHeader.put("Host", "api.gotokeep.com");
                return super.getHeaders();
            }
        };
        volleySingleton._addRequest(newestDataGsonRequest);
    }
}
